package com.group.mall.controller;

import com.group.mall.domain.DTO.OrderDTO;
import com.group.mall.domain.POJO.OrderDetail;
import com.group.mall.domain.VO.OrderShowVO;
import com.group.mall.domain.VO.OrderVO;
import com.group.mall.service.OrdersService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {


    @Autowired
    private OrdersService ordersService;
    //查询所有订单
    @RequestMapping("/orders/{id}")
    public List<OrderDetail> getAllOrders(@PathVariable("id")Long id) {
        log.info("查询所有订单");
        List<OrderDetail> orderList = ordersService.getAllOrders(id);
        return orderList;
    }

    //根究订单状态查询订单
    @RequestMapping("/orders/{userId}/{status}")
    public List<OrderDetail> getOrderByStatus(@PathVariable("userId")Long userId, @PathVariable("status")Integer status) {
        log.info("查询订单状态为{}的订单",status);
        log.info("查询用户{}的订单",userId);
        List<OrderDetail> orderList = new ArrayList<>();
        if(status == 0){
            orderList = ordersService.getAllOrders(userId);
        }else if(status >= 0){
            orderList = ordersService.getOrderByStatus(userId, status);
        }
        return orderList;
    }

    //搜索订单
    @RequestMapping("/search/{userId}/{keyword}")
    public List<OrderDetail> searchOrder(@PathVariable("userId")Long userId, @PathVariable("keyword")String keyword) {
        log.info("搜索订单");
        log.info("搜索用户{}的订单", userId);
        log.info("搜索关键字{}", keyword);
        List<OrderDetail> orderList = ordersService.searchOrder(userId, keyword);
        return orderList;
    }

    //新增订单
    @PostMapping("/addOrder")
    public OrderShowVO addOrder(OrderDTO orderDTO) {
        log.info("新增订单");
        log.info("订单信息{}", orderDTO);
        OrderShowVO orderShowVO = ordersService.addOrder(orderDTO);
        return orderShowVO;
    }

    //取消订单
    @RequestMapping("/cancel/{orderId}")
    public boolean cancelOrder(@PathVariable("orderId") Long orderId){
        Boolean result = ordersService.cancelOrder(orderId);
        return result;
    }
    //支付
    @RequestMapping("/pay/{orderId}")
    public boolean pay(@PathVariable("orderId") Long orderId){
        Boolean result = ordersService.pay(orderId);
        return result;
    }
    //确认收货
    @RequestMapping("/confirm/{orderId}")
    public boolean confirm(@PathVariable("orderId") Long orderId){
        Boolean result = ordersService.confirm(orderId);
        return result;
    }
    //退款
    @RequestMapping("/refund/{orderId}")
    public boolean refund(@PathVariable("orderId") Long orderId){
        Boolean result = ordersService.refund(orderId);
        return result;
    }
    //删除订单记录
    @RequestMapping("/delate/{orderId}")
    public boolean delate(@PathVariable("orderId") Long orderId){
        Boolean result = ordersService.delate(orderId);
        return result;
    }

}
